What is @istanbuljs/load-nyc-config?
The @istanbuljs/load-nyc-config package is designed to load the configuration for nyc, a popular code coverage tool used in JavaScript projects. It helps in parsing and assembling the configuration from various sources such as package.json, .nycrc, .nycrc.json, and nyc.config.js files. This package simplifies the process of managing and utilizing nyc configurations, making it easier to integrate code coverage into your testing workflow.
What are @istanbuljs/load-nyc-config's main functionalities?
Loading configuration from a file
This feature allows you to load the nyc configuration asynchronously from the default or specified configuration files within a project directory. It searches for configuration in common file formats and merges them according to precedence.
const loadNycConfig = require('@istanbuljs/load-nyc-config');
async function getConfig() {
const config = await loadNycConfig({cwd: '/path/to/project'});
console.log(config);
}
getConfig();
Merging custom options with file-based configuration
This demonstrates how to merge custom configuration options with those loaded from configuration files. It's useful for programmatically adjusting the nyc configuration in scripts or tools.
const loadNycConfig = require('@istanbuljs/load-nyc-config');
async function getConfigWithOverrides() {
const options = {reporter: ['lcov', 'text']};
const config = await loadNycConfig({cwd: '/path/to/project'}, options);
console.log(config);
}
getConfigWithOverrides();
Other packages similar to @istanbuljs/load-nyc-config
nyc
While nyc itself is primarily a command-line tool for capturing code coverage, it inherently deals with loading and applying configuration for its operation. Compared to @istanbuljs/load-nyc-config, nyc offers a broader set of functionalities including instrumentation, reporting, and configuration management as part of its core features.
config
The 'config' package is used for managing configurations across Node.js applications. It doesn't specifically target nyc configurations but provides a general-purpose solution for loading and merging configurations from various sources. Unlike @istanbuljs/load-nyc-config, it's not tailored to code coverage tools but can be adapted for various configuration needs.
@istanbuljs/load-nyc-config
The utility function which NYC uses to load configuration.
This can be used by outside programs to calculate the configuration.
Command-line arguments are not considered by this function.
const {loadNycConfig} = require('@istanbuljs/load-nyc-config');
(async () {
console.log(await loadNycConfig());
})();
loadNycConfig([options])
options.cwd
Type: string
Default: cwd
from parent nyc process or process.cwd()
options.nycrcPath
Type: string
Default: undefined
Name of the file containing nyc configuration.
This can be a relative or absolute path.
Relative paths can exist at options.cwd
or any parent directory.
If an nycrc is specified but cannot be found an exception is thrown.
If no nycrc option is provided the default priority of config files are:
- .nycrc
- .nycrc.json
- .nycrc.yml
- .nycrc.yaml
- nyc.config.js
- nyc.config.cjs
- nyc.config.mjs
Configuration merging
Configuration is first loaded from package.json
if found, this serves as the package
defaults. These options can be overridden by an nycrc if found. Arrays are not merged,
so if package.json
sets "require": ["@babel/register"]
and .nycrc
sets "require": ["esm"]
the effective require setting will only include "esm"
.
isLoading
const {isLoading} = require('@istanbuljs/load-nyc-config');
console.log(isLoading());
In some cases source transformation hooks can get installed before the configuration is
loaded. This allows hooks to ignore source loads that occur during configuration load.
@istanbuljs/load-nyc-config
for enterprise
Available as part of the Tidelift Subscription.
The maintainers of @istanbuljs/load-nyc-config
and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.